package com.china.service.impl;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.china.dao.LongLatDaoI;
import com.china.model.LongLat;
import com.china.pageModel.LngLat;
import com.china.pageModel.base.DataGrid;
import com.china.service.LongLatServiceI;

@Service("longLatService")
public class LongLatServiceImpl implements LongLatServiceI {
	
	private LongLatDaoI longLatDao;

	public LongLatDaoI getLongLatDao() {
		return longLatDao;
	}
	/**
	 * 自动注入数据层
	 * @param longLatDao
	 */
	@Autowired
	public void setLongLatDao(LongLatDaoI longLatDao) {
		this.longLatDao = longLatDao;
	}
	


	@Override
	public DataGrid datagrid(LngLat lngLat) {
		// TODO Auto-generated method stub
		DataGrid j = new DataGrid();
		j.setRows(this.changModel(this.find(lngLat)));
		j.setTotal(this.total(lngLat));
		return j;
	}
	
	/**
	 * 私有的为datagrid的提供服务
	 * @param longLat 数据库的原始模型
	 * @return 页面的数据模型
	 */
	private List<LngLat> changModel(List<LongLat> longLat){
		List<LngLat> lngLats = new ArrayList<LngLat>();
		
		if(longLat != null && longLat.size() > 0){
			for(LongLat tl : longLat ){
				LngLat l = new LngLat();
				BeanUtils.copyProperties(tl,l); //复制属性
				lngLats.add(l);
			}
		}
		return lngLats;
	}
	/**
	 * 根据前台的参数查找数据库的模型
	 * @param lngLat
	 * @return
	 */
	private List<LongLat> find(LngLat lngLat){
		String hql = "from LongLat t where 1 = 1";
		Map<String,Object> params = new HashMap<String, Object>();
		
		hql = (String) addWhere(lngLat,hql,params).get("hql");
		
		params = (Map<String, Object>) addWhere(lngLat,hql,params).get("params");
		
		if(null != lngLat.getSort() && null != lngLat.getOrder()){
			hql += " order by "+ lngLat.getSort() + " "+lngLat.getOrder();
		}
		return this.longLatDao.find(hql, params, lngLat.getPage(), lngLat.getRows());
	}
	
	/**
	 * 添加条件
	 * @param lngLat
	 * @param hql
	 * @param params
	 * @return
	 */
	private Map<String, Object> addWhere(LngLat lngLat, String hql, Map<String, Object> params) {
		
		Map<String,Object> newParams = new HashMap<String,Object>();
		if(null != lngLat.getCounty()){
			hql += " and t.county like :county ";
			params.put("county", "%%"+lngLat.getCounty().trim()+"%%");
		}
		if (lngLat.getCcreatedatetimeStart() != null) {
			hql += " and t.ccreatedatetime>=:ccreatedatetimeStart ";
			params.put("ccreatedatetimeStart",lngLat.getCcreatedatetimeStart());
		}
		if (lngLat.getCcreatedatetimeEnd() != null) {
			hql += " and t.ccreatedatetime<=:ccreatedatetimeEnd ";
			params.put("ccreatedatetimeEnd",lngLat.getCcreatedatetimeEnd());
		}
		if (lngLat.getCmodifydatetimeStart() != null) {
			hql += " and t.cmodifydatetime>=:cmodifydatetimeStart ";
			params.put("cmodifydatetimeStart",lngLat.getCmodifydatetimeStart());
		}
		if (lngLat.getCmodifydatetimeEnd() != null) {
			hql += " and t.cmodifydatetime<=:cmodifydatetimeEnd ";
			params.put("cmodifydatetimeEnd",lngLat.getCmodifydatetimeEnd());
		}
		newParams.put("hql",hql);
		newParams.put("params", params);
		return newParams;
	}

	private Long total(LngLat lngLat) {
		// TODO Auto-generated method stub
		String hql = "select count(*) from LongLat t where 1=1 ";
		Map<String, Object> params = new HashMap<String, Object>();
		hql = (String) this.addWhere(lngLat, hql, params).get("hql");
		params = (Map<String, Object>) this.addWhere(lngLat, hql, params).get("params");
		return this.longLatDao.count(hql, params);
	}


	@Override
	public LngLat get(LngLat lngLat) {
		// TODO Auto-generated method stub
		return null;
	}

	
	
}
